package com.zgm.utils.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zgm.utils.DensityUtil;
import com.zgm.utils.R;
import com.zgm.utils.UiUtils;

/**
 * Created by 张公铭 on 2017/7/18 0018.
 */

public class UiSearchHeader extends LinearLayout
{
    private Context mContext;
    private RelativeLayout rl_toot;
    private View view_bar;
    private EditText ed_search;
    private ImageView iv_left;
    private NumIcon iv_right_one;
    private NumIcon iv_right_two;
    private IOnSearchListener listener;

    public interface IOnSearchListener
    {
        void searchText(String text);
    }

    public void setListener(IOnSearchListener listener)
    {
        this.listener = listener;
    }

    public UiSearchHeader(Context context)
    {
        this(context, null);
    }

    public UiSearchHeader(Context context, @Nullable AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public UiSearchHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init()
    {
        LayoutInflater.from(mContext).inflate(R.layout.search_header, this);
        rl_toot = (RelativeLayout) findViewById(R.id.rl_search_header_root);
        view_bar = findViewById(R.id.view_search_header_bar);
        view_bar.setVisibility(GONE);
        ed_search = (EditText) findViewById(R.id.ed_header_search);
        ed_search.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                if (actionId == EditorInfo.IME_ACTION_SEARCH)
                {
                    if (listener != null)
                    {
                        listener.searchText(v.getText().toString());
                        return true;
                    }
                }
                return false;
            }
        });
        Drawable drawable = getResources().getDrawable(R.drawable.wrait_search);
        drawable.setBounds(0, 0, DensityUtil.dip2px(mContext, 16), DensityUtil.dip2px(mContext, 16));
        ed_search.setCompoundDrawables(drawable, null, null, null);
        UiUtils.editTextFocus(ed_search);

        iv_left = (ImageView) findViewById(R.id.iv_search_header_left);
        iv_right_one = (NumIcon) findViewById(R.id.iv_search_header_right_one);
        iv_right_two = (NumIcon) findViewById(R.id.iv_search_header_right_two);
    }

    public void setLeftButtonIcon(int resId)
    {
        iv_left.setImageResource(resId);
    }

    public void setRightOneButtonIcon(int resId)
    {
        iv_right_one.setImageResource(resId);

    }

    public void setRightTwoButtonIcon(int resId)
    {
        iv_right_two.setImageResource(resId);
    }

    public void setEditTextBackgroudTrans()
    {
        ed_search.setBackgroundResource(R.drawable.search_edittext_trans_bk);
        iv_right_one.setImageResource(R.drawable.wrait_cart);
        ed_search.setHintTextColor(Color.parseColor("#ffffff"));
        ed_search.setTextColor(Color.parseColor("#ffffff"));
        Drawable drawable = getResources().getDrawable(R.drawable.wrait_search);
        drawable.setBounds(0, 0, DensityUtil.dip2px(mContext, 16), DensityUtil.dip2px(mContext, 16));
        ed_search.setCompoundDrawables(drawable, null, null, null);
    }

    public void setEditTextBackgroud()
    {
        ed_search.setBackgroundResource(R.drawable.search_edittext_bk);
        iv_right_one.setImageResource(R.drawable.black_cart);
        ed_search.setHintTextColor(Color.parseColor("#999999"));
        ed_search.setTextColor(Color.parseColor("#999999"));
        Drawable drawable = getResources().getDrawable(R.drawable.black_search);
        drawable.setBounds(0, 0, DensityUtil.dip2px(mContext, 16), DensityUtil.dip2px(mContext, 16));
        ed_search.setCompoundDrawables(drawable, null, null, null);
    }

    public void setRootBackgour(int id)
    {
        rl_toot.setBackgroundResource(id);
    }

    public void setLeftButtonOnClickListener(OnClickListener listener)
    {
        iv_left.setOnClickListener(listener);
    }

    public void setRightButtonOneOnClickListener(OnClickListener listener)
    {
        iv_right_one.setOnClickListener(listener);
    }

    public void setRightButtonTwoOnClickListener(OnClickListener listener)
    {
        iv_right_two.setOnClickListener(listener);
    }

    public void hindLeftButton()
    {
        iv_left.setVisibility(GONE);
    }

    public void hindRightButtonOne()
    {
        iv_right_one.setVisibility(GONE);
    }

    public void hindRightButtonOneNum()
    {
        iv_right_one.setNum(0);
    }

    public void setRightButtonOneNum(int num)
    {
        iv_right_one.setNum(num);
    }

    public void hindRightButtonTwo()
    {
        iv_right_two.setVisibility(GONE);
    }

    public void hindRightButtonTwoNum()
    {
        iv_right_one.setNum(0);
    }

    public void setRightButtonTwoNum(int num)
    {
        iv_right_one.setNum(num);
    }

    public void setEdittextHind(String hindText)
    {
        ed_search.setHint(hindText);
    }

    public void setEdittextDeafalutText(String text)
    {
        ed_search.setText(text);
    }

    public void showBarView(int height)
    {
        view_bar.setVisibility(VISIBLE);
        ViewGroup.LayoutParams params = view_bar.getLayoutParams();
        params.height = height;
        view_bar.setLayoutParams(params);
    }

    public void editeRequestFocus()
    {
        UiUtils.editTextFocus(ed_search);
    }
}
